package com.xqiapp.xframe.plugin.codes.util;

import java.util.Map;
import java.util.Properties;

/**
 * 读取配置文件
 * 
 * @author mike
 *
 */
public class PropsUtil {

	private static final Properties props;

	static {
		props = LoadProps.loadProps("codes/codes.properties");
	}

	/**
	 * 获取对应的key值
	 * 
	 * @param props
	 * @param key
	 * @return
	 */
	public static String getString(String key) {
		if (props.containsKey(key))
			return props.getProperty(key);
		return null;
	}

	/**
	 * 获取带有默认值的key值
	 * 
	 * @param props
	 * @param key
	 * @param defVal
	 * @return
	 */
	public static String getString(String key, String defVal) {
		if (props.containsKey(key))
			return props.getProperty(key);
		return defVal;
	}

	/**
	 * 
	 * @param props
	 * @param key
	 * @return
	 */
	public static int getInt(Properties props, String key) {
		if (props.containsKey(key))
			return CaseUtil.toInt(props.get(key));
		return 0;
	}

	/**
	 * 
	 * @param props
	 * @param key
	 * @param defVal
	 * @return
	 */
	public static int getInt(String key, int defVal) {
		if (props.containsKey(key))
			return CaseUtil.toInt(props.get(key));
		return defVal;
	}

	/**
	 * 
	 * @param props
	 * @param key
	 * @return
	 */
	public static long getLong(Properties props, String key) {
		if (props.containsKey(key))
			return CaseUtil.toLong(props.get(key));
		return 0l;
	}

	/**
	 * 
	 * @param props
	 * @param key
	 * @param defVal
	 * @return
	 */
	public static long getLong(String key, int defVal) {
		if (props.containsKey(key))
			return CaseUtil.toLong(props.get(key));
		return defVal;
	}

	/**
	 * 
	 * @param props
	 * @param key
	 * @return
	 */
	public static boolean getBoolean(String key) {
		return getBoolean(key, false);
	}

	/**
	 * 
	 * @param props
	 * @param key
	 * @param defVal
	 * @return
	 */
	public static boolean getBoolean(String key, boolean defVal) {
		if (props.containsKey(key))
			return CaseUtil.toBoolean(props.get(key));
		return defVal;
	}

	/**
	 * 获取指定的相关前缀
	 * 
	 * @param prefix
	 * @return
	 */
	public static Map<String, Object> getMap(String prefix) {
		return LoadProps.getMap(props, prefix);
	}

}
